#!/bin/bash
#
# zlib support, with installer
#
# Usage:
#   mkl_require zlib
#
# And then call the following function from the correct place/order in checks:
#   mkl_check zlib
#

mkl_toggle_option "Feature" ENABLE_ZLIB "--enable-zlib" "Enable support for zlib compression" "try"

function manual_checks {
    case "$ENABLE_ZLIB" in
        n) return 0 ;;
        y) local action=fail ;;
        try) local action=disable ;;
        *) mkl_err "mklove internal error: invalid value for ENABLE_ZLIB: $ENABLE_ZLIB"; exit 1 ;;
    esac

    mkl_meta_set "zlib" "apk" "zlib-dev"
    mkl_meta_set "zlib" "static" "libz.a"
    mkl_lib_check "zlib" "WITH_ZLIB" $action CC "-lz" \
                  "
#include <zlib.h>

void foo (void) {
     z_stream *p = NULL;
     inflate(p, 0);
}
"
}


# Install zlib from source tarball
#
# Param 1: name (zlib)
# Param 2: install-dir-prefix (e.g., DESTDIR)
# Param 2: version (optional)
function install_source {
    local name=$1
    local destdir=$2
    local ver=1.2.11

    echo "### Installing $name $ver from source to $destdir"
    if [[ ! -f Makefile ]]; then
        curl -fL https://zlib.net/zlib-${ver}.tar.gz | \
            tar xzf - --strip-components 1
    fi

    CFLAGS=-fPIC ./configure --static --prefix=/usr
    make -j
    make test
    make DESTDIR="${destdir}" install
    return $?
}
